package com.yequan.spring.bean.definition;

import com.yequan.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * 构建BeanDefinition示例
 *
 * @author : Administrator
 * @date : 2020/3/31
 */
public class BeanDefinitionCreationDemo {

    public static void main(String[] args) {
        //1.通过BeanDefinitionBuilder 构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        //通过属性设置
        beanDefinitionBuilder.addPropertyValue("id", 1).addPropertyValue("name", "叶权");
        //获取BeanDefinition实例
        BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        //此时的BeanDefinition不是终态

        //2.通过AbstractBeanDefinition 以及其派生类构建
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        //设置Bean类型
        genericBeanDefinition.setBeanClass(User.class);
        //通过 MutablePropertyValues 批量设置bean属性
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue("id", 1);
        propertyValues.addPropertyValue("name", "叶权");
        //批量操作属性
        genericBeanDefinition.setPropertyValues(propertyValues);

    }

}
